import 'package:flutter/material.dart';

/// 有按压效果的widget，通过materialColor设置按压颜色
class MaterialLayout extends StatelessWidget {
  const MaterialLayout({
    super.key,
    this.disabledTextColor,
    this.backgroundColor,
    this.disabledBackgroundColor,
    this.minHeight = 20.0,
    this.minWidth = 20,
    this.padding = EdgeInsets.zero,
    this.radius = 0.0,
    this.side = BorderSide.none,
    required this.onPressed,
    required this.child,
    this.materialColor,
    this.borderRadius,
    this.onLongPress,
  });

  final Color? disabledTextColor;
  final Color? backgroundColor;
  final Color? disabledBackgroundColor;
  final Color? materialColor;
  final double? minHeight;
  final double? minWidth;
  final VoidCallback? onPressed;
  final VoidCallback? onLongPress;
  final EdgeInsetsGeometry padding;
  final BorderSide side;
  final Widget child;
  final double radius;
  final BorderRadiusGeometry? borderRadius;

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: onPressed,
        onLongPress: onLongPress,
        style: ButtonStyle(
            backgroundColor: WidgetStateProperty.resolveWith((states) {
              if (states.contains(WidgetState.disabled)) {
                return disabledBackgroundColor ?? Colors.black12;
              }
              return backgroundColor ?? Colors.white;
            }),
            overlayColor: WidgetStateProperty.resolveWith((states) {
              return materialColor ?? Theme.of(context).splashColor;
            }),
            minimumSize: (minWidth == null || minHeight == null)
                ? null
                : WidgetStateProperty.all<Size>(Size(minWidth!, minHeight!)),
            padding: WidgetStateProperty.all<EdgeInsetsGeometry>(padding),
            shape: WidgetStateProperty.all<OutlinedBorder>(
              RoundedRectangleBorder(
                borderRadius: borderRadius ?? BorderRadius.circular(radius),
              ),
            ),
            side: WidgetStateProperty.all<BorderSide>(side),
            tapTargetSize: MaterialTapTargetSize.shrinkWrap),
        child: child);
  }
}
